/*
 * @Description: 
 * @version: 
 * @Author: liurui
 * @Date: 2024-01-28 14:49:49
 * @LastEditors: 
 * @LastEditTime: 2024-01-30 15:48:53
 */
import {createRouter, createWebHashHistory, Router} from 'vue-router'

let modules = import.meta.glob('../src/**/*.vue')

let baseModules = import.meta.glob('../src/practice-base/**/*.vue')
let midModules = import.meta.glob('../src/practice-mid/**/*.vue')
let heightModules = import.meta.glob('../src/practice-height/**/*.vue')

function generateRoutes(modules) {
    return Object.keys(modules).map(key => {
        let path = key.replace(/(\.\/|\.vue)/g, '')
        let pathArr = path.split('/')
        let name = pathArr[pathArr.length - 1].split('.')[0]
        
        return {
            name,
            path: `/${name}`,
            component: modules[key]
        }
    })
}
/**
 * 动态生成路由，组件名称不可重复
 */
let baseRoutes = generateRoutes(baseModules)
let midRoutes = generateRoutes(midModules)
let heightRoutes = generateRoutes(heightModules)

let routes = [
    {
		name: 'index',
		path: '/index',
		component: modules['../src/views/index.vue']
	},
    ...baseRoutes,
    ...midRoutes,
    ...heightRoutes
]

let router: Router = createRouter({
    routes: routes, // 路由配置数组
    history: createWebHashHistory() // 创建一个hash模式的history对象
})

export default router